<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */
namespace app\api\controller;
use app\common\model\Article as ArticleModel;
use app\common\model\Material as MaterialModel;
use app\common\model\Card as CardModel;

class Article extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /* 文章列表 */
    public function list () {
      $input = input();
      $page = max($input['page'],1);
      $size = 20;
      $where = [];
      $list = ArticleModel::getPage($where,$page,$size,'ord desc,id desc');
      if(!$list) {
        $this->ajax(200,'success',[]);
      }
      foreach($list as $k=>$v) {
        $list[$k]['picture'] = tomedia($v['picture']);
      }
      $this->ajax(200,'success',$list);
    }

    /* 文章详情 */
    public function detail () {
      $input = input();
      if(!$input['id']) {
        $this->ajax(202,'参数错误');
      }
      $detail = ArticleModel::getOne(['id'=>$input['id']]);
      if(!$detail) {
        $this->ajax(202,'参数错误');
      }
      $detail['picture'] = tomedia($detail['picture']);
      $this->ajax(200,'success',$detail);
    }

    /* 素材列表 */
    public function materialList () {
      $input = input();
      $page = max($input['page'],1);
      $size = 20;
      $where = [];
      $list = MaterialModel::getPage($where,$page,$size,'ord desc,id desc');
      if(!$list) {
        $this->ajax(200,'success',[]);
      }
      foreach($list as $k=>$v) {
        $pics = json_decode($v['picture_list'],true);
        foreach($pics as $kk=>$vv) {
          $pics[$kk]['src'] = tomedia($vv['path']);
        }
        $list[$k]['picture_list'] = $pics;
      }
      $this->ajax(200,'success',$list);
    }

    /* 单条素材 */
    public function materialDetail () {
      $input = input();
      if(!$input['id']) {
        $this->ajax(202,'参数错误');
      }
      $detail = MaterialModel::getOne(['id'=>$input['id']]);
      if(!$detail) {
        $this->ajax(202,'参数错误');
      }
      $pics = json_decode($detail['picture_list'],true);
      foreach($pics as $kk=>$vv) {
        $pics[$kk]['src'] = tomedia($vv['path']);
      }
      $detail['picture_list'] = $pics;
      $this->ajax(200,'success',$detail);
    }

    /* 邀请卡列表 */
    public function cardList () {
      $input = input();
      $page = max($input['page'],1);
      $size = 100;
      $where = [];
      $list = CardModel::getPage($where,$page,$size,'ord desc,id desc');
      if(!$list) {
        $this->ajax(200,'success',[]);
      }
      foreach($list as $k=>$v) {
        $list[$k]['bg_picture'] = tomedia($v['bg_picture']);
        $content = json_decode($v['content'],true);
        foreach($content['list'] as $kk=>$vv) {
          if($vv['type']=='img') {
            $content['list'][$kk]['content'] = tomedia($vv['content']);
          }
        }
        $list[$k]['content'] = $content;
      }
      $this->ajax(200,'success',$list);
    }
}